<?php

namespace App\V3\V3_2_7\Controllers;

use App\Http\Controllers\Controller;
use App\Http\Requests\V3_2_7\BatchUpdShelfStatusRequest;
use App\Http\Requests\V3_2_7\LocationOutInStorageRequest;
use App\Http\Requests\V3_2_7\PageRequest;
use App\Http\Requests\V3_2_7\VerifyIdRequest;
use App\Services\CommonService;
use App\V3\V3_2_7\Models\StorageShelfLocationModel;
use App\V3\V3_2_7\Models\StorageShelfModel;
use App\V3\V3_2_7\Services\StorageShelfLocationService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Validator;

class StorageShelfLocationController extends BaseController
{
    /**
     *仓库货架库位列表
     * Author: zlf
     * Created at 2024/8/24 15:39
     * @param Request $request
     * @return JsonResponse
     */
    public function getList(Request $request): JsonResponse
    {
        $storageIdArr = [];
        $data = $request->all();
        $condition = [];

        if ($this->getMainUserId() === $this->getUserId() || $this->isShareStorage()) {
            $condition['main_id'] = $this->getMainUserId();
        } else {
            $userInfo = [
                'main_id' => $this->getMainUserId(),
                'user_id' => $this->getUserId()
            ];
            $storageIdArr = CommonService::getMultiUser($userInfo);
            if (empty($storageIdArr)) {
                $condition['user_id'] = $this->getUserId();
            }
        }
        $data['host'] = $request->getSchemeAndHttpHost();
        if (!empty($data['keyword'])) $data['main_id'] = $this->getMainUserId();
        return success(StorageShelfLocationService::newTwoList($data, $condition, $storageIdArr));
    }

    /**
     *货架库位列表
     * Author: zlf
     * Created at 2024/9/6 15:14
     * @param VerifyIdRequest $request
     * @return JsonResponse
     */
    public function getShelfLocaList(VerifyIdRequest $request): JsonResponse
    {
        $data = $request->all();
        $data['user_id'] = $this->getUserId();
        $data['main_id'] = $this->getMainUserId();

        return $this->success(StorageShelfLocationService::getShelfLocaList($data));
    }

    /**
     *仓库、货架、库位的批量删除
     * Author: zlf
     * Created at 2024/8/27 11:01
     * @param Request $request
     * @return JsonResponse
     */
    public function batchDel(Request $request): JsonResponse
    {
        $data = $request->all();
        $data['time'] = time();
        $data['user_id'] = $this->getUserId();
        $data['main_id'] = $this->getMainUserId();
        CommonService::log('删除-仓库货架库位', $data);
        return Cache::lock(__CLASS__ . __FUNCTION__ . $data['user_id'], 5)
            ->get(function () use ($data) {
                // 锁定 10 秒并自动释放...
                return StorageShelfLocationService::batchDel($data);
            });
    }

    /**
     *修改货架
     * Author: zlf
     * Created at 2024/9/5 15:20
     * @param Request $request
     * @return JsonResponse
     */
    public function shelfUpdate(Request $request): JsonResponse
    {
        $validator = Validator::make($data = $request->all(), [
            'shelfName' => 'required|string',//货架名称
            'shelfId' => 'required|integer',//货架ID
        ], [
            'shelfName.required' => '参数`shelfName`缺失',
            'shelfName.string' => '参数`shelfName`仅能为字符串',
            'shelfId.required' => '参数`shelfId`缺失',
            'shelfId.integer' => '参数`shelfId`仅能为数字',
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }
        $data['time'] = time();
        $data['user_id'] = $this->getUserId();
        $data['main_id'] = $this->getMainUserId();
        $this->operateLog('修改柜子', $data, $data['time']);
        return Cache::lock(__CLASS__ . __FUNCTION__ . $data['user_id'], 5)
            ->get(function () use ($data) {
                return StorageShelfLocationService::updShelf($data);
            });
    }

    /**
     * 添加库位
     */
    public function updStorLocation(Request $request)
    {
        $validator = Validator::make($data = $request->all(), [
            'locationName' => 'required|string',//库位名称
            'locationId' => 'required',//库位ID
        ], [
            'locationName.required' => '参数`locationName`缺失',
            'locationName.string' => '参数`locationName`仅能为字符串',
            'locationId.required' => '参数`locationId`缺失',
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        $data['time'] = time();
        $data['user_id'] = $this->getUserId();
        $data['main_id'] = $this->getMainUserId();
        $this->operateLog('修改库位', $data, $data['time']);
        return Cache::lock(__CLASS__ . __FUNCTION__ . $data['user_id'], 5)
            ->get(function () use ($data) {
                return StorageShelfLocationService::updLocation($data);
            });
    }

    /**
     *修改货架、库位的状态
     * Author: zlf
     * Created at 2024/8/27 11:01
     * @param VerifyIdRequest $request
     * @return JsonResponse
     */
    public function batchUpdShelfStatus(VerifyIdRequest $request): JsonResponse
    {
        $data = $request->all();
        $data['time'] = time();
        $data['user_id'] = $this->getUserId();
        $data['main_id'] = $this->getMainUserId();
        CommonService::log('修改货架、库位的状态', $data);
        return Cache::lock(__CLASS__ . __FUNCTION__ . $data['user_id'], 5)
            ->get(function () use ($data) {
                // 锁定 10 秒并自动释放...
                return StorageShelfLocationService::batchUpdShelfStatus($data);
            });
    }

    /**
     *仓库使用的标签列表
     * Author: zlf
     * Created at 2024/8/29 16:54
     * @param Request $request
     * @return JsonResponse
     */
    public function getStorageTagList(Request $request)
    {
        $data['main_id'] = $this->getMainUserId();
        return $this->success(StorageShelfLocationService::getCustomTagList($data));

    }

    /**
     *库位出入库
     * Author: zlf
     * Created at 2024/9/6 17:34
     * @param LocationOutInStorageRequest $request
     */
    public function locationOutInGoodsList(LocationOutInStorageRequest $request): JsonResponse
    {
        $data = $request->all();
        $data['time'] = time();
        $data['user_id'] = $this->getUserId();
        $data['main_id'] = $this->getMainUserId();
        return $this->success(StorageShelfLocationService::outInGoodsList($data));
    }

    public function transferShelf(Request $request)
    {
        $data = $request->all();
        $data['time'] = time();
        $data['user_id'] = $this->getUserId();
        $data['main_id'] = $this->getMainUserId();
        CommonService::log('货架、库位转移', $data);
        return Cache::lock(__CLASS__ . __FUNCTION__ . $data['user_id'], 5)
            ->get(function () use ($data) {
                // 锁定 10 秒并自动释放...
                return StorageShelfLocationService::transferShelf($data);
            });
    }

    public function getIndex(Request $request)
    {
        $data = $request->all();
        $data['user_id'] = $this->getUserId();
        $data['main_id'] = $this->getMainUserId();
        return $this->success(StorageShelfLocationService::getIndex($data));
    }
}
